const dbpool = require('../config/dbpoolconfig');

const transactionController = {
    // 交易流水列表
    transactionList(req, res) {
        dbpool.connect('select * from transaction', [], (err, data) => {
            if (!err) {
                res.send(data);
            }
        })
    },
    // 搜索
    transactionListSearch(req, res) {
        let userId = req.query.userId || '';
        let orderId = req.query.orderId || '';
        let transactionBill = req.query.transactionBill || '';
        let type = req.query.type || '';
        let style = req.query.style || '';
        let mode = req.query.mode || '';
        let time = req.query.transactionDate || '';


        let sql = 'select * from transaction where 1=1 ';
        let params = [];

        if (userId.trim() != '') {
            sql += 'and userId like ? ';
            userId = '%' + userId.trim() + '%';
            params.push(userId)
        }
        if (orderId.trim() != '') {
            sql += 'and orderId like ?';
            orderId = '%' + naorderIdme.trim() + '%';
            params.push(orderId)
        }

        if (transactionBill.trim() != '') {
            sql += 'and transactionBill like ?';
            transactionBill = '%' + transactionBill.trim() + '%';
            params.push(transactionBill)
        }
        if (type != '' && type != '全部') {
            sql += 'and type = ?';
            params.push(type)
        }

        if (style != '' && style != '全部') {
            if(style == '银行卡'){
                style = '来聊'
            }
            sql += 'and style = ? ';
            params.push(style)
        }
        if (mode != '' && mode != '全部') {
            sql += 'and mode = ? ';
            params.push(mode)
        }
        if (time != '') {
            sql += 'and time between ? and ? ';
            params.push(time[0], time[1])
        }

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                res.send(data);
            }
        })

    },

    // 提现已处理
    treatedList(req, res) {
        let result = []
        dbpool.connect('select * from transaction', [], (err, data) => {
            if (!err) {
                data.forEach(item => {
                    if (item.type == '提现' && item.mode != '待处理') {
                        if (item.style == '来聊') {
                            item.style = '银行卡'
                        }
                        result.push(item)
                    }
                })
                res.send(result)

            }
        })
    },
    // 搜索
    treatedListSearch(req, res) {
        let userId = req.query.userId || '';
        let orderId = req.query.orderId || '';
        let transactionBill = req.query.transactionBill || '';
        let style = req.query.style || '';
        let handleMode = req.query.handleMode || '';
        let time = req.query.transactionDate || '';
        let handleTime = req.query.handleDate || '';

        let result = []
        let sql = 'select * from transaction where 1=1 ';
        let params = [];

        if (userId.trim() != '') {
            sql += 'and userId like ? ';
            userId = '%' + userId.trim() + '%';
            params.push(userId)
        }
        if (orderId.trim() != '') {
            sql += 'and orderId like ?';
            orderId = '%' + naorderIdme.trim() + '%';
            params.push(orderId)
        }

        if (transactionBill.trim() != '') {
            sql += 'and transactionBill like ?';
            transactionBill = '%' + transactionBill.trim() + '%';
            params.push(transactionBill)
        }

        if (style != '' && style != '全部') {
            if (style == '银行卡') {
                style = '来聊'

            }
            sql += 'and style = ? ';
            params.push(style)

        }
        if (handleMode != '' && handleMode != '全部') {
            sql += 'and handleMode = ? ';
            params.push(handleMode)
        }
        if (time != '') {
            sql += 'and time between ? and ? ';
            params.push(time[0], time[1])
        }
        if (handleTime != '') {
            sql += 'and handleTime between ? and ? ';
            params.push(handleTime[0], handleTime[1])
        }

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                data.forEach(item => {
                    if (item.type == '提现' && item.mode != '待处理') {
                        if (item.style == '来聊') {
                            item.style = '银行卡'
                        }
                        result.push(item)
                    }
                })
                res.send(result)
            }
        })
    },
    // 提现待处理
    waitTreatList(req, res) {
        let result = []
        dbpool.connect('select * from transaction', [], (err, data) => {
            if (!err) {
                data.forEach(item => {
                    if (item.type == '提现' && item.mode == '待处理') {
                        if (item.style == '来聊') {
                            item.style = '银行卡'
                        }
                        result.push(item)
                    }
                })
                res.send(result)
            }
        })
    },
    // 搜索
    waitTreatListSearch(req, res) {
        let userId = req.query.userId || '';
        let orderId = req.query.orderId || '';
        let transactionBill = req.query.transactionBill || '';
        let style = req.query.style || '';
        let time = req.query.transactionDate || '';

        let result = []
        let sql = 'select * from transaction where 1=1 ';
        let params = [];

        if (userId.trim() != '') {
            sql += 'and userId like ? ';
            userId = '%' + userId.trim() + '%';
            params.push(userId)
        }
        if (orderId.trim() != '') {
            sql += 'and orderId like ?';
            orderId = '%' + naorderIdme.trim() + '%';
            params.push(orderId)
        }

        if (transactionBill.trim() != '') {
            sql += 'and transactionBill like ?';
            transactionBill = '%' + transactionBill.trim() + '%';
            params.push(transactionBill)
        }

        if (style != '' && style != '全部') {
            if (style == '银行卡') {
                style = '来聊'

            }
            sql += 'and style = ? ';
            params.push(style)

        }
        if (time != '') {
            sql += 'and time between ? and ? ';
            params.push(time[0], time[1])
        }

        dbpool.connect(sql, params, (err, data) => {
            if (!err) {
                data.forEach(item => {
                    if (item.type == '提现' && item.mode == '待处理') {
                        if (item.style == '来聊') {
                            item.style = '银行卡'
                        }
                        result.push(item)
                    }
                })
                res.send(result)
            }
        })
    },
    // 打款
    paymentTreat(req, res) {
        let id = req.query.id || req.body.id;
        let mode = req.query.mode || '';
        let handleMode = req.query.handleMode || '';
        let handleTime = req.query.handleTime || '';
        let next = req.query.next || '';
        let back = req.query.back || '';



        // 获取当前日期
        let date = new Date();
        let year = date.getFullYear();
        let month = (date.getMonth() + 1).toString().padStart(2, 0);
        let day = date.getDate().toString().padStart(2, 0);
        let hour = date.getHours().toString().padStart(2, 0);
        let minute = date.getMinutes();
        let time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute

        let params= [];
        mode = '成功'
        handleMode = '打款成功'
        handleTime = ''+time+''
        next = ''
        back = ''
        params.push(mode,handleMode,handleTime,next,back,id)
        

        dbpool.connect('UPDATE transaction SET mode = ?,handleMode=?,handleTime = ?,next=?,back=? WHERE id = ?',params, (err, data) => {
            if (!err) {
                res.send('success')
            }
        })
    },

    // 驳回
    backTreat(req, res) {
        let id = req.query.id || req.body.id;
        let mode = req.query.mode || '';
        let handleMode = req.query.handleMode || '';
        let handleTime = req.query.handleTime || '';
        let next = req.query.next || '';
        let back = req.query.back || '';



        // 获取当前日期
        let date = new Date();
        let year = date.getFullYear();
        let month = (date.getMonth() + 1).toString().padStart(2, 0);
        let day = date.getDate().toString().padStart(2, 0);
        let hour = date.getHours().toString().padStart(2, 0);
        let minute = date.getMinutes();
        let time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute

        let params= [];
        mode = '失败'
        handleMode = '已驳回'
        handleTime = ''+time+''
        next = ''
        back = ''
        params.push(mode,handleMode,handleTime,next,back,id)
        

        dbpool.connect('UPDATE transaction SET mode = ?,handleMode=?,handleTime = ?,next=?,back=? WHERE id = ?',params, (err, data) => {
            if (!err) {
                res.send('success')
            }
        })
    },
   
}

module.exports = transactionController;